function hasClass(target, name) {
   return target.className.match(new RegExp('(\\s|^)' + name + '(\\s|$)'));
}
function removeClass(target, name) {
   if (hasClass(target, name)) {
       target.className = target.className.replace(new RegExp('(\\s|^)' + name + '(\\s|$)'), ' ');
   }
}
function addClass(target, name) {
   if (!hasClass(target, name)) {
       target.className += " " + name;
   }
} 
//google map v3右键菜单控件
var menuControl = function(map) {
	this.container = document.createElement("div");
	this.coordinate = null;
	this.map = map;
	this.sender = map
	this.isEnable = true;
	this.container.className = "menu_casing";
	this.hide();
	this.items = new Array();
	var self = this;
	google.maps.event.addListener(map, "click", function() {
	   self.hide();
	});
	google.maps.event.addListener(map, "movestart", function() {
	   self.hide();
	});
	google.maps.event.addListener(map, "zoom_changed", function() {
	   self.hide();
	});
	google.maps.event.addListener(map, "dragstart", function() {
	   self.hide();
	});
	this.container.index = 1;
	map.controls[google.maps.ControlPosition.TOP_RIGHT].push(this.container);
}

menuControl.prototype = {
	//显示菜单
	show: function(groupName) {
   	this.container.style.display = "block";
		if (groupName) {
		   for (var i = 0; i < this.items.length; i++) {
			   if (groupName == this.items[i].groupName) {
			       this.items[i].show();
			   } else {
			       this.items[i].hide();
			   }
		   }
		}
	},
	//隐藏菜单
	hide: function(groupName) {
		this.container.style.display = "none";
		if (groupName) {
		   for (var i = 0; i < this.items.length; i++) {
			   if (groupName == this.items[i].groupName) {
			       this.items[i].hide();
			   } else {
			       this.items[i].show();
			   }
		   }
		}
	},
	//启用菜单
	enable: function() {
   	this.isEnable = true;
	},
	//禁用菜单
	disable: function() {
   	this.isEnable = false;
	},
	//判断当前菜单是否隐藏
	isHide: function() {
  	return (this.container.style.display == "none");
	},
	//增加菜单项
	addItem: function(item) {
		item.host = this;
		this.items.push(item);
		this.container.appendChild(item.casing);
	},
	//增加菜单项分割符号
	addSeparator: function(group) {
		var separator = group || new menuSeparator();
		if (typeof group == "string") {
		   separator = new menuSeparator(group);
		}
		this.items.push(separator);
		this.container.appendChild(separator.casing);
	}
} 
//菜单分隔符
var menuSeparator = function(groupName){
	this.groupName = groupName;
	this.casing = document.createElement("div");
	this.casing.className = "menu_separator";
}

menuSeparator.prototype = {
	show: function(){
		this.casing.style.display = "block";
	},
	hide: function() {
		this.casing.style.display = "none";
	}
}
//菜单项
var menuItem = function(options) {
   options = options || {};
   this.text = options.text || "";
   this.icon = options.icon;
   this.handler = options.handler;
   this.groupName = options.groupName;
   this.host = null; //宿主
   this.casing = document.createElement("div");
   this.casing.className = "menu_item";
   var menu_text = document.createElement("div");
   menu_text.className = "menu_text";
   var text_lable = document.createElement("span");
   text_lable.innerHTML = this.text;
   menu_text.appendChild(text_lable);
   this.casing.appendChild(menu_text);

   var self = this;
   if (this.icon) {
       var item_icon = document.createElement("div");
       item_icon.className = "menu_icon";
       item_icon.style.backgroundImage = "url(" + self.icon + ")";
       self.casing.appendChild(item_icon);
   }

   if (typeof self.handler == "function") {
       google.maps.event.addDomListener(self.casing, "click", function() {
           if (self.host) {
               self.handler(self.host.coordinate);
               self.host.hide();
           }
       });
   }
   google.maps.event.addDomListener(self.casing, "mouseover", function() {
       addClass(self.casing, "item_active");
   });

   google.maps.event.addDomListener(self.casing, "mouseout", function() {
       removeClass(self.casing, "item_active");
   });
}
menuItem.prototype = {
	show: function(){
		this.casing.style.display = "block";
	},
	hide: function() {
		this.casing.style.display = "none";
	}
}